'use strict'

define([ 'jquery', 'bootstrap', 'bootstrapTable', 'bootstrapTableCN',
    'datetimepicker', 'datetimepickerCN', 'select2', 'validate',
    'validate.en', 'shiftform', ], function($) {

  return {

    init : function(data) {
      this.initPage(data.day, data.period, data.today, data.source);
      this.exportReport();
      this.initAddService();
    },

    initPage : function(day, period, today, source) {
      $('.date').datetimepicker({
          format : 'yyyy-mm-dd',
          autoclose : true,
          language : 'zh-CN',
          todayBtn : 'linked',
          todayHighlight : true,
          pickerPosition : 'bottom-right',
          minView : 'month',
          startDate : '2017-12-05',
          endDate : today
        });
      
      $('select[name="source"]').select2({
        placeholder : '请选择来源',
        minimumResultsForSearch : -1,
        allowClear : false
      });

      $('input[name="day"]').change(function() {
        window.location.href = window.__CTX__.root
            + "/taste-stat-reports?day=" + $(this).val()
            + "&period=" + period + "&source=" + source;
      });
      
      $('select[name="source"]').change(function() {
          window.location.href = window.__CTX__.root
              + "/taste-stat-reports?day=" + day
              + "&period=" + period + "&source=" + $(this).val();
        });
    },

    exportReport : function() {
      var that = this
      $(document).on(
          'shown.bs.modal',
          '#report-export',
          function(e) {

            $(e.currentTarget).find('form[data-validate="true"]')
                .bt_validate() // bind validate

            $('button[type="submit"').click(
                function() {
                  $(e.currentTarget).find("form").trigger(
                      "validate.ajax.submit");
                  $.bt_validate.result[$.bt_validate.form_id]
                })

          })
    },
    
    initAddService: function(){
        $(document).on('shown.bs.modal', '#add-service', function(e){
            $(e.currentTarget).find("form").shiftform()
        });
    }
  }
})
